package com.xinsoft.controller.device;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.CusUpkeepPlanDTO;
import com.xinsoft.entity.dto.CusUpkeepPlanParam;
import com.xinsoft.entity.po.CusUpkeepPlan;
import com.xinsoft.service.CusUpkeepPlanService;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CusUpKeepPlanController
 * @Description 保养计划控制层
 * @Author lijie
 * @Date 2023年6月14日09:58:303
 * @Version 1.0
 */
@RestController
@RequestMapping("/cus/upKeep/plan")
@Api(value = "CusUpKeepPlanController", tags = {"保养计划控制层"})
public class CusUpKeepPlanController {
    @Autowired
    private CusUpkeepPlanService upkeepPlanService;


    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 保养计划-暂存
     * @Date 2023/5/17 9:57
     * @params dept
     **/
    @ApiOperation("暂存")
    @PostMapping("/add")
    public Response<?> add(@RequestBody CusUpkeepPlanDTO cusUpkeepPlan) {
        // 参数校验
        ValidatorUtils.validateEntity(cusUpkeepPlan);
        CusUpkeepPlan oldPlan = upkeepPlanService.getOne(new QueryWrapper<CusUpkeepPlan>().eq("plan_name", cusUpkeepPlan.getPlanName()));
        if (oldPlan != null && !oldPlan.getId().equals(cusUpkeepPlan.getId())) {
            return Response.fail("该项保养计划已存在,请不要重复添加");
        }
        upkeepPlanService.tempSave(cusUpkeepPlan);
        return Response.succeed("操作成功");
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 保养计划-提交
     * @Date 2023/5/17 9:57
     * @params dept
     **/
    @ApiOperation("提交")
    @PostMapping("/submit")
    public Response<?> submit(@RequestBody CusUpkeepPlanDTO cusUpkeepPlan) {
        // 参数校验
        ValidatorUtils.validateEntity(cusUpkeepPlan);
        CusUpkeepPlan oldPlan = upkeepPlanService.getOne(new QueryWrapper<CusUpkeepPlan>().eq("plan_name", cusUpkeepPlan.getPlanName()));
        if (oldPlan != null && !oldPlan.getId().equals(cusUpkeepPlan.getId())) {
            return Response.fail("该项保养计划已存在,请不要重复添加");
        }
        upkeepPlanService.submit(cusUpkeepPlan);
        return Response.succeed("操作成功");
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 更新
     * @Date 2023/5/17 9:57
     * @params dept
     **/
    @ApiOperation("更新")
    @PostMapping("/edit")
    public Response<?> edit(@RequestBody CusUpkeepPlan cusUpkeepPlan) {
        // 参数校验
        ValidatorUtils.validateEntity(cusUpkeepPlan);
        CusUpkeepPlan oldPlan = upkeepPlanService.getOne(new QueryWrapper<CusUpkeepPlan>().eq("plan_name", cusUpkeepPlan.getPlanName()));
        if (oldPlan != null && !oldPlan.getId().equals(cusUpkeepPlan.getId())) {
            return Response.fail("该项保养计划已存在,请不要重复添加");
        }
        return Response.succeed(upkeepPlanService.updateById(cusUpkeepPlan));
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 获取详情
     * @Date 2023/5/17 9:57
     * @params id
     **/
    @ApiOperation("获取详情")
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        CusUpkeepPlan plan = upkeepPlanService.getByInfoById(id);
        return Response.succeed(plan);
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 删除
     * @Date 2023/5/12 9:20
     * @params ids
     **/
    @ApiOperation("删除")
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Integer> ids) {
        for (int i = 0; i < ids.size(); i++) {
            CusUpkeepPlan plan = upkeepPlanService.getByInfoById(ids.get(i));
            if (plan.getDoAble().equals(Const.AUDIT_STATUS_NORMAL)) {
                return Response.fail("存在已启用的计划，无法删除");
            }
        }
        upkeepPlanService.removeByIds(ids);
        return Response.succeed();
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 删除
     * @Date 2023/5/12 9:20
     * @params ids
     **/
    @ApiOperation("批量启用")
    @PostMapping("/pick")
    public Response<?> pick(@RequestBody List<Integer> ids) {
        List<CusUpkeepPlan> list = new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            CusUpkeepPlan cusUpkeepPlan = new CusUpkeepPlan();
            cusUpkeepPlan.setId(ids.get(i));
            cusUpkeepPlan.setDoAble(Const.AUDIT_STATUS_NORMAL);
            list.add(cusUpkeepPlan);
        }
        upkeepPlanService.updateBatchById(list);
        return Response.succeed();
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 删除
     * @Date 2023/5/12 9:20
     * @params ids
     **/
    @ApiOperation("批量禁用")
    @PostMapping("/ban")
    public Response<?> ban(@RequestBody List<Integer> ids) {
        List<CusUpkeepPlan> list = new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            CusUpkeepPlan cusUpkeepPlan = new CusUpkeepPlan();
            cusUpkeepPlan.setId(ids.get(i));
            cusUpkeepPlan.setDoAble(Const.AUDIT_STATUS_BAN);
            list.add(cusUpkeepPlan);
        }
        upkeepPlanService.updateBatchById(list);
        return Response.succeed();
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 保养计划分页
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @PostMapping("/pageList")
    public Response<?> pageList(@Valid @RequestBody CusUpkeepPlanParam param) {
        return Response.succeed(upkeepPlanService.findPageListByParam(param));
    }

}
